#include<iostream>
#include<set>
#include<map>
#include<list>
#include<string>
#include<vector>
#include<sstream>
#include<algorithm> 
#include<utility>
#include<fstream>
#include<sstream>
#include<iterator>
#include <stdexcept>
#include"TestSalesData.cpp"
using namespace std;
void practice_11_16();
void practice_11_17();
void practice_11_20();
void practice_11_23();
void practice_11_28();
void practice_11_31();
void practice_11_32();
void practice_11_33();
const string &transform(const string &s,const map<string,string> &m);
map<string,string> buildMap(ifstream &map_file);
void word_transform(ifstream &map_file,ifstream &input);
int main()
{
	//practice_11_16();
	//practice_11_17();
	//practice_11_20();
	//practice_11_23();
	//practice_11_28();
	//practice_11_31();
	//practice_11_32();
	practice_11_33();
	return 0;
}
void practice_11_16()
{
	 map<string,int> word_count={{"zyl",2},{"hello",3},{"Tom",4}};
	 auto it=word_count.begin();
	 while(it!=word_count.end()){
	 	it->second=2;
	 }

}
void practice_11_17()
{
	multiset<string> c{"zyl","zzz","jkoad","lokmn"};
	multiset<string> c1=c;
	vector<string> v{"zzzz","zxc","xcvv","vbnm","asdf"};
	vector<string> v1=v;
	copy(v.begin(),v.end(),inserter(c,c.end()));
	for(auto i:c)cout<<i<<" ";
	cout<<endl;
	c=c1;
	// copy(v.begin(),v.end(),back_inserter(c));
	// for(auto i:c)cout<<i<<" ";
	// cout<<endl;
	// c=c1;
	copy(c.begin(),c.end(),inserter(v,v.end()));
	for(auto i:v)cout<<i<<" ";
	cout<<endl;
	v=v1;
	copy(c.begin(),c.end(),back_inserter(v));
	for(auto i:v)cout<<i<<" ";
	cout<<endl;
	v=v1;
}
void practice_11_20()
{
	map<string,size_t> word_count;
	string word;
	while(cin>>word)
	{
		auto ret=word_count.insert({word,1});
		if(!ret.second){
			++ret.first->second;
		}
	}		
}
void practice_11_23()
{
	multimap<string,string> member;
	string line,first,second;
	vector<string> v;
	ifstream input("Member.txt");
	while(getline(input,line))
		v.push_back(line);
	for(const string &s:v){
		istringstream iss(s);
		iss>>first>>second;
		member.insert({first,second});
	}
	for(const pair<string,string> &p:member)
	{
		cout<<p.first<<" "<<p.second;
		cout<<endl;
	}
}
void practice_11_28()
{
	map<string,vector<int>> m;
	vector<int> v1{1,4,5,6};
	vector<int> v2{2,7,9,10};
	m.insert(make_pair("zyl",v1));
	m.insert(make_pair("zyl111ok",v2));
	auto rt=m.find("zyl");
	cout<<rt->second[2]<<endl;
}
void practice_11_31()
{
	multimap<string,string> m;
	m.insert(make_pair("zyl","Java"));
	m.insert(make_pair("zyl","Cpp"));
	m.insert(make_pair("zyl","CSharp"));
	m.insert(make_pair("zyl","Python"));
	string zyl("zyl");
	auto cnt=m.count(zyl);
	auto it=m.find(zyl);
	while(cnt){
		it=m.erase(it);
		--cnt;
	}
	cout<<m.size()<<endl;
}
void practice_11_32()
{
	multimap<string,string> m;
	m.insert(make_pair("zyl","Java"));
	m.insert(make_pair("zyl","Cpp"));
	m.insert(make_pair("zyl","CSharp"));
	m.insert(make_pair("zyl","Python"));
	string zyl("zyl");
	auto it=m.find(zyl);
	auto cnt=m.count(zyl);
	cout<<it->first<<" ";
	while(cnt){
		cout<<it->second<<" ";
		++it;
		--cnt;
	}
}
void word_transform(ifstream &map_file,ifstream &input)
{
	auto tran_map=buildMap(map_file);
	string text;
	while(getline(input,text)){
		istringstream stream(text);
		string word;
		bool firstword=true;
		while(stream>>word){
			if(firstword)
				firstword=false;
			else
				cout<<" ";
			cout<<transform(word,tran_map);
		}
		cout<<endl;
	}
}
map<string,string> buildMap(ifstream &map_file)
{
	map<string,string> rule_map;
	string key;
	string value;
	while(map_file>>key&&getline(map_file,value))
	{
		if(value.size()>1)
			rule_map[key]=value.substr(1);
		else
			throw runtime_error("no rule for "+key);
	}
	return rule_map;
}
const string &transform(const string &s,const map<string,string> &m)
{
	auto map_it=m.find(s);
	if(map_it!=m.cend())
		return map_it->second;
	else
		return s;
}
void practice_11_33()
{
	ifstream map_file("rule.txt");
	ifstream input("inputfile.txt");
	word_transform(map_file,input);
}




